//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System.Configuration;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Management.Instrumentation;

namespace EntLibContrib.Query.Configuration.Manageability.Queries
{
	/// <summary>
	/// Represents the configuration information from a 
	/// <see cref="EntLibContrib.Query.Configuration.CustomQueryData"/> instance.
	/// </summary>
	/// <seealso cref="QueryBaseSetting"/>
	[ManagementEntity]
	public class CustomQuerySetting : QueryBaseSetting
	{
		#region Fields
		private string[] attributes;
		private string queryType;
		#endregion

		#region Properties
		/// <summary>
		/// Gets the collection of attributes for the custom query represented as a
		/// <see cref="string"/> array of key/value pairs for the represented configuration object.
		/// </summary>
		/// <value>The attributes.</value>
		/// <seealso cref="EntLibContrib.Query.Configuration.CustomQueryData.Attributes">CustomQueryData.Attributes</seealso>
		[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
		[ManagementConfiguration]
		public string[] Attributes
		{
			get { return attributes; }
			set { attributes = value; }
		}

		/// <summary>
		/// Gets the name of the type for the custom query for the represented configuration object.
		/// </summary>
		/// <value>The type of the query.</value>
		/// <seealso cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.NameTypeConfigurationElement.Type">Inherited NameTypeConfigurationElement.Type</seealso>
		[ManagementConfiguration]
		public string QueryType
		{
			get { return queryType; }
			set { queryType = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="CustomQuerySetting"/> class with a name, the provider type,
		/// and the attributes.
		/// </summary>
		/// <param name="sourceElement">The source element.</param>
		/// <param name="name">The name of the resource provider.</param>
		/// <param name="queryType">Type of the query.</param>
		/// <param name="attributes">The attributes for the provider.</param>
		public CustomQuerySetting(CustomQueryData sourceElement,
															string name,
															string queryType,
															string[] attributes)
			: base(sourceElement, name)
		{
			this.queryType = queryType;
			this.attributes = attributes;
		}
		#endregion

		#region Public Static Methods
		/// <summary>
		/// Returns an enumeration of the published <see cref="CustomQuerySetting"/> instances.
		/// </summary>
		/// <returns></returns>
		[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate"), ManagementEnumerator]
		public static IEnumerable<CustomQuerySetting> GetInstances()
		{
			return GetInstances<CustomQuerySetting>();
		}

		/// <summary>
		/// Returns the <see cref="CustomQuerySetting"/> instance corresponding to the provided values for the key properties.
		/// </summary>
		/// <param name="applicationName">Name of the application.</param>
		/// <param name="sectionName">Name of the section.</param>
		/// <param name="name">The name.</param>
		/// <returns>
		/// The published <see cref="CustomQuerySetting"/> instance specified by the values for the key properties,
		/// or <see langword="null"/> if no such an instance is currently published.
		/// </returns>
		[ManagementBind]
		public static CustomQuerySetting BindInstance(string applicationName,
																								 string sectionName,
																								 string name)
		{
			return BindInstance<CustomQuerySetting>(applicationName, sectionName, name);
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Saves the changes on the <see cref="CustomQuerySetting"/> to its corresponding configuration object.
		/// </summary>
		/// <param name="sourceElement">The configuration object on which the changes must be saved.</param>
		/// <returns>
		/// 	<see langword="true"/> if changes have been saved, <see langword="false"/> otherwise.
		/// </returns>
		/// <remarks>Must be overridden by subclasses to perform the actual save.</remarks>
		protected override bool SaveChanges(ConfigurationElement sourceElement)
		{
			return CustomQueryDataWmiMapper.SaveChanges(this, sourceElement);
		}
		#endregion
	}
}
